from Modules.databases_service import Config


def insert_genre(data: dict) -> None:
    Config().db_insert('GENRES', ['name', 'url', 'img'], data)


def add_update_favorite_genres(data: list) -> None:
    Config().db_update('GENRES', 'favorite', 0, 'favorite is TRUE')
    for item in data:
        Config().db_update('GENRES', 'favorite', 1, f"name = '{item['name']}' and url = '{item['url']}'")


def get_all_genres() -> list:
    result: list = []
    items: list = Config().db_select("SELECT * FROM GENRES")
    attrs: list = Config().get_columns('GENRES')
    for item in items:
        dictionary: dict = {}
        for attr in enumerate(attrs):
            dictionary.setdefault(attr[1], item[attr[0]])
        result.append(dictionary)
    return result


def get_favorites_genres() -> list:
    result: list = []
    items: list = Config().db_select("SELECT * FROM GENRES WHERE FAVORITE is TRUE")
    attrs: list = Config().get_columns('GENRES')
    for item in items:
        dictionary: dict = {}
        for attr in enumerate(attrs):
            dictionary.setdefault(attr[1], item[attr[0]])
        result.append(dictionary)
    return result
